package com.example.yadmin.sys.controller;

import com.example.yadmin.common.vo.Result;
import com.example.yadmin.sys.entity.Cart;
import com.example.yadmin.sys.entity.Chat;
import com.example.yadmin.sys.mapper.ChatMapper;
import com.example.yadmin.sys.service.IChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ag
 * @since 2023-11-04
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private IChatService chatService;

    @Autowired
    private ChatMapper chatMapper;

    @GetMapping("/all")
    public Result<List<Chat>> getAllChat(){
        List<Chat> list = chatService.list();
        return Result.success(list,"查询成功");
    }
    //增
    @PostMapping
    public Result<?> addChat(@RequestBody Chat chat){
        LocalDateTime formatter = LocalDateTime.now();
        chat.setTime(formatter);
        chatService.addChat(chat);
        return Result.success("增加一条信息成功");
    }
    //删
    @DeleteMapping("/{id}")
    public Result<?> deleteChatById(@PathVariable("id")Integer id){
        chatService.deleteChatById(id);
        return Result.success("删除消息成功");
    }
    @GetMapping("/time")
    public Result<List<Chat>> getChatGroupByTime(){
        List<Chat> list =chatMapper.getChatGroupByTime();
        return Result.success(list);
    }
    //根据会话id获取当前会话所有信息
    @GetMapping
    public Result<List<Chat>> getChatByTalkId(@RequestParam(value = "talkId")Integer talkId){
        List<Chat> allChat = chatMapper.getChatByTalkId(talkId);
        return Result.success(allChat);
    }
    //根据会话id获取开始时间到结束时间的所有信息
    @GetMapping("/chatInfo")
    public Result<List<Chat>> getChatInfoByTalkId(@RequestParam(value = "talkId")Integer talkId){
        List<Chat> allChat = chatMapper.getChatInfoByTalkId(talkId);
        return Result.success(allChat);
    }
    @PutMapping
    public Result<?> updateChat(@RequestBody Chat chat){
        chatService.updateChat(chat);
        return Result.success("修改消息成功");
    }

//    @GetMapping("/{id}")
//    public Result<Chat> getChatById(@PathVariable("id")Integer id){
//       Chat chat = chatService.getChatById(id);
//       return Result.success(chat);
//    }

}
